package com.dimples.dd.web.framework.jackson.core;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 基于时间戳的 LocalDateTime 序列化器
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/8/26
 */
public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {

    public static final TimestampLocalDateTimeSerializer INSTANCE = new TimestampLocalDateTimeSerializer();

    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        // 将 LocalDateTime 对象，转换为 日期 格式
        gen.writeString(DateUtil.format(value, DatePattern.NORM_DATETIME_PATTERN));
    }

}
